package com.example.demo.service;

import com.example.demo.domain.Teacher;
import com.example.demo.respository.TeacherRepository;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;



@Service
public class TeacherService {

    @Resource
    private TeacherRepository teacherRepository;

    // 查询所有的教师
    public List<Teacher> findAllTeacher(){
        return this.teacherRepository.findAll();
    }

    /**
     * 根据id 查询教师
     * @param id
     * @return
     */
    public Teacher findTeacherById(Integer id){
        return this.teacherRepository.findById(id).orElse(null);
    }


    /**
     * 根据学工号 密码查询
     * @param sn
     * @param password
     * @return
     */
    public Teacher findBySnAndPassword(String sn,String password){
       return this.teacherRepository.findBySnAndPassword(sn,password);
    }


    /**
     * 根据学工号密码查询
     * @param phone
     * @param password
     * @return
     */
    public Teacher findByPhoneAndPassword(String phone,String password){
        return this.teacherRepository.findByPhoneAndPassword(phone,password);
    }


    /**
     * 保存教师信息
     * @param teacher
     */
    public void saveTeacher(Teacher teacher){
        this.teacherRepository.save(teacher);
    }

    /**
     * 修改教师信息
     * @param teacher
     */
    public void updateTeacher(Teacher teacher){
        this.teacherRepository.save(teacher);
    }


    /**
     * 删除教师信息
     */
    public void deleteTeacher(Teacher teacher){
        this.teacherRepository.delete(teacher);
    }


    public List<Teacher> findMaxSn(){
        return this.teacherRepository.findMaxSn();
    }


    /**
     * 根据姓名查询
     * @param name
     * @return
     */
    public List<Teacher> findByNameLike(String name){
        return this.teacherRepository.findByNameLike(name);
    }

    public  void saveInfo(Teacher teacher) {
        this.teacherRepository.save(teacher);
    }
}
